package cn.tedu.cake.controller;

import cn.tedu.cake.ex.ServiceException;
import cn.tedu.cake.web.JsonResult;
import cn.tedu.cake.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult  handlerServiceException(ServiceException e){
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e){
        String message ="服务器忙,请稍后再试!";
        log.warn("执行过程中出现了Throwable错误");
        log.warn("异常类型为:{}",e.getClass().getName());
        log.warn("异常信息:{}",e.getMessage());
        log.warn("异常跟踪信息如下:",e);
        return JsonResult.fail(ServiceCode.ERR_UNKNOWN,message);
    }
}
